/*********************************************************************************************************
**
**                                   GJB 标准测试集
**
**                               Copyright All Rights Reserved
**
**--------------文件信息--------------------------------------------------------------------------------
**
** 文   件   名: gjb_S0100901GN_5.c
**
** 文件创建日期: 2021 年 1 月 22 日
**
** 描        述: 初始化、销毁条件变量功能测试
*********************************************************************************************************/
#define __SYLIXOS_KERNEL
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <limits.h>

//#include "gjb.h"

#define TEST "-2"
#define FUNCTION "pthread_cond_destroy"
#define ERROR_PREFIX "unexpected error: " FUNCTION " " TEST ": "

static  int   err_count_cv_9015;
/* cond used by the two threads */
pthread_cond_t cond_cv_9015 = PTHREAD_COND_INITIALIZER;

/* cond used by the two threads */
pthread_mutex_t mutex_cv_9015 = PTHREAD_MUTEX_INITIALIZER;

void * thread_cv_9015(void *tmp)
{
    int    rc = 0;

    /*
     * acquire the mutex
     */
    rc  = pthread_mutex_lock(&mutex_cv_9015);  
    if(rc != 0) {
        printf(ERROR_PREFIX "pthread_mutex_lock\n");
        err_count_cv_9015++;
        return (void *)-1;
    }

    /*
     * Wait on the cond var. This will not return, as nobody signals
     */
    rc = pthread_cond_wait(&cond_cv_9015, &mutex_cv_9015);
    if(rc != 0) {
        printf(ERROR_PREFIX "pthread_cond_wait\n");
        err_count_cv_9015++;
        return (void *)-1;
    }

    rc = pthread_mutex_unlock(&mutex_cv_9015);
    if(rc != 0) {
        printf(ERROR_PREFIX "pthread_mutex_unlock\n");
        err_count_cv_9015++;
        return (void *)-1;
    }

    return NULL;
}

int gjb_S0100901GN_5(int argc, char **argv)
{
    pthread_t low_id;
    int       rc = 0;

    /*
     * Create a new thread with default attributes
     */
    rc = pthread_create(&low_id, NULL, thread_cv_9015, NULL);
    if(rc != 0) {
        printf(ERROR_PREFIX "pthread_create\n");
        goto __errno_handle;
    }

    /*
     * Let the other thread run
     */
    sleep(5);


    /*
     * Try to destroy the cond var. This should return an error
     */
    rc = pthread_cond_destroy(&cond_cv_9015);
    if(rc != EBUSY) {
        printf(ERROR_PREFIX "Test failed: Expected %d(EBUSY) got %d.\n", EBUSY, rc);
        goto __errno_handle;
    }

    //GJB_PRT_INFO("Test PASS\n");
    printf("Test PASS\n");

    rc = pthread_cond_signal(&cond_cv_9015);
    if(rc != 0) {
        printf("pthread_cond_signal\n");
        goto __errno_handle;
    }

    if (err_count_cv_9015) {
        goto __errno_handle;
    }
    printf("<RSLT>..................................................[(0)]\n \n \n");

    return  (0);

__errno_handle:
    printf("<RSLT>..................................................[(-1)]\n \n \n");
    return  (-1);
}
